04 .FCM测试

FCM 测试

Legacy http 升级到 Http v1

如果应用使用的是 FCM 旧版 HTTP API,您应该考虑按照本指南中的说明改为使用 HTTP v1 API。与旧版 API 相比,HTTP v1 API 具有以下优势:

旧版本使用 secret key,新版本使用 access token,1 个小时有效期,更安全。

升级参考:由使用旧版 HTTP 改为使用 HTTP v1

如何获取 Secret Key

提供 Server Key:Firebase Console→Settings→Clound Messaging项目设置/FCM设置

如何获取 Access Token

项目设置→服务账号→Firebase Admin SDK

Firebase Admin SDK 服务账号

代码获取

FCM 测试

Firebase 控制台: Notifications Composer

  1. 添加通知内容

tujmw

  1. 添加 FCM Token(通过代码获取Token

rlo13

  1. 单条消息收起和展开

csehk

19zvg

  1. 多条

6bvx8

OAuth 2.0 Playground + HTTP v 1

旧版本的 FCM APIs 在 2024 年 6 月过期,需要升级到新版本的 HTTP v1

FCM HTTP v 1 + OAuth 2.0 Playground

获取 project id

0qapa

Step 1:选择要授权的 API

Select & authorize APIs

  1. Open Google Developers OAuth 2.0 Playground https://developers.google.com/oauthplayground
  2. 勾选 "Firebase Cloud Messaging API v1" 下的两项 (auth/clound-platformauth/firebase.messaging), and click "Authorize API’s" button

lan2z

  1. You will be redirected to Authentication and needs permission for Google OAuth 2.0 Playground to view and manage the GCP services. Click "Allow" button.

u4yja

  1. 跳转到 Step2

6lbfi

Step 2:用 authorization code 获取 Refresh/Access Token

Exchange authorization code for tokens

从步骤 1 获得 Authorization Code 后,单击 Exchange authorization code for tokens 按钮,您将获得 OAuth 受保护资源所需的访问令牌:refresh and an access token

13u2v

jxmgd

Step 3:发送请求

Configure request to API) and add the following details

lynfk

如:https://fcm.googleapis.com/v1/projects/the-monkey-king-assistant/messages:send

{
    "message": {
        "token": "FCM_TOKEN",
        "data": {
            "body": "Body of Your Notification in data",
            "title": "Title of Your Notification in data",
            "key_1": "Value for key_1",
            "key_2": "Value for key_2",
            "deeplink": "hacket://me/main"
        }
    }
}

17rv2

c4p5m

Postman + HTTP v 1

{
  "message": {
    "token": "FCM_TOKEN",
    "data": {
      "body": "Body of Your Notification in data",
      "title": "Title of Your Notification in data",
      "key_1": "Value for key_1",
      "key_2": "Value for key_2"
    }
  }
}

nrwcg

Terminal curl + HTTP v 1

Send Notification using Terminal

curl --location --request POST 'https://fcm.googleapis.com/v1/projects/<Project Id>/messages:send' \
--header 'Authorization: Bearer <Access token>' \
--header 'Content-Type: application/json' \
--data-raw '{
"message": {
"token": "FCM_TOKEN",
"data": {
"body": "Body of Your Notification in data",
"title": "Title of Your Notification in data",
"key_1": "Value for key_1",
"key_2": "Value for key_2"
}
}
}'

三方

  1. 提供 Server Key:Firebase Console→Settings→Clound Messaging项目设置/FCM设置
  2. FCM Token

具体的 json 格式,可参考https://firebase.google.com/docs/cloud-messaging/concept-options

{
    "title": "The notification's title1",
    "body": "The notification's body text1",
    "key1": "Custom value11",
    "key2": "Custom value22"
}
{
	"token": "fLoH_IY_T5-nuQFY3m_s_z:APA91bE3pClY8cUpwgt-3gRp5w8Lw6Hy0GqGEZ4JZwU0L6lJMmbeFsZjEN6upZOGyUF8QbrVMOCBL5TDyynP7tTQAqTFizXcYLevqVi_Ao88BcOyhRUV0GoA9NIVlIoRFsitbKBe9U1U",
	"notification": {
		"title": "Match update",
		"body": "Arsenal goal in added time, score is now 3-0"
	},
	"android": {
		"ttl": "30s",
		"notification": {
			"click_action": "OPEN_ACTIVITY_1"
		}
	},
  "data": {
            "body": "Body of Your Notification in data",
            "title": "Title of Your Notification in data",
            "key_1": "Value for key_1",
            "key_2": "Value for key_2"
        }
}

fcm-tester

fcm-tester

  1. 获取 secret-key
  2. 获取 fcm token
  3. 填写 push 消息

代码

官方文档:构建应用服务器发送请求  |  Firebase Cloud Messaging

Java

NodeJs

notification 和 data 消息测试

notification 消息

data 消息

Ref